
const model = require('../schems')
const tagsAction = require('./tag')






let acticleAction = {}

function gethumbnail(str) {
  console.log(str)
  let pattern = /!\[(.*?)\]\((.*?)\)/mg;
  let result = [];
  let matcher;
  
  while ((matcher = pattern.exec(str)) !== null) {
    result.push(matcher[2]);
  }
  if(pattern.test(str) == false){
    return '';
  }
  if(result.length>=3){
    var temp =[result[0],result[1],result[2]]
    return temp
  }else{
    return [result[0]]
  }
}

//新文章页相关信息
acticleAction.home = async function (callback) {
  let user, cate;
  await model.user.find({}, function (err, doc) {
    if (!err) {
      user = doc
    }
  })
  await model.category.find({}, function (err, doc) {
    if (!err) {
      cate = doc
    }
  })
  await callback(user, cate)
}


// 增加文章
acticleAction.add = async function (obj, callback) {
  let user, cate, arctile;
  let newtags = obj.tags.split(',') || ''; //获取tag数组
  let taginfo = await tagsAction.tagChuli(null, newtags)
  await model.user.find({}, function (err, doc) {
    if (!err) {
      user = doc
    }
  })
  await model.category.find({}, function (err, doc) {
    if (!err) {
      cate = doc
    }
  })
  let tagString
  taginfo ? obj.tags = taginfo.tagsId : delete obj.tags;
  obj.thumbnail = gethumbnail(obj.content.toString())
  let actile = new model.article(obj);
  taginfo ? tagString = taginfo.oldtags.toString() : tagString = '';
  await actile.save().then(e => { arctile = JSON.parse(JSON.stringify(e)) })
  if (user && cate && arctile) callback(user, cate, arctile, tagString);

}


//修改文章
acticleAction.edit = async function (_id, obj, callback) {
  let user, cate, arctile;
  console.log(obj.tags)
  let newtags = obj.tags && obj.tags.split(',');
  let taginfo = await tagsAction.tagChuli(_id, newtags)
  //用户信息
  await model.user.find({}, function (err, doc) {
    if (!err) {
      user = doc
    }
  })

  //分类信息
  await model.category.find({}, function (err, doc) {
    if (!err) {
      cate = doc
    }
  })
  obj.thumbnail = gethumbnail(obj.content.toString())
  obj.tags = taginfo.tagsId
  //更新文章
  await model.article.findOneAndUpdate({ _id }, obj, { new: true }, function (err, result) {
    if (result) {
      arctile = JSON.parse(JSON.stringify(result))
    }
  })
  let tagString = taginfo.oldtags.toString()
  if (user && cate && arctile) await callback(user, cate, arctile, tagString);
}

//列举所有文章
acticleAction.all = function (callback) {
  model.article.find().populate('authorId', 'name').populate('categoryId','name').sort({'createAt':-1}).exec(function (err, doc) {
    //  console.log(doc);
    callback(doc)
  })
}

//根据id查询文章
acticleAction.arcById = async function (_id, callback) {
  let user, cate, arctile;
  await model.user.find({}, function (err, doc) {
    if (!err) {
      user = doc
    }
  })
  await model.category.find({}, function (err, doc) {
    if (!err) {
      cate = doc
    }
  })

  let allTag = {}
  await model.tag.find({}, function (err, doc) {
    if (doc) {
      doc.forEach(item => {
        allTag[item._id] = item.tagName
      })
    }
  })
  let tagString = [];
  var tagStr
  await model.article.findOne({ _id }, function (err, doc) {
    if (!err) {
      doc.tags.forEach(item => {
        tagString.push(allTag[item])
      })
      arctile = JSON.parse(JSON.stringify(doc))
      tagStr = tagString.toString()
    }
  })
  tagStr = tagString.toString()
  if (user && cate && arctile) callback(user, cate, arctile, tagStr);
}



//删除文章
acticleAction.delete = function (_id, callback) {
  model.article.deleteOne({
    _id
  }, function (err) {
    if (!err) {
      callback(true)
    }
  })
}

module.exports = acticleAction